﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;

namespace SimpleTerminal.Controls
{
    [ToolboxItem(true)]
    class FontComboBox : BaseComboBox
    {
        public event EventHandler SelectFontChanged;

        protected bool monospaced = true;

        public FontComboBox() : base()
        {

        }

        protected override void UpdateData()
        {
            base.UpdateData();
            Items.Clear();
            CultureInfo currentCulture = CultureInfo.CurrentUICulture;
            CultureInfo enUsCultureInfo = new CultureInfo("en-US");
            foreach (var family in Fonts.SystemFontFamilies)
            {
                foreach (var keyPair in family.FamilyNames)
                {
                    var specificCulture = keyPair.Key.GetSpecificCulture();
                    if (!specificCulture.Equals(currentCulture) && !specificCulture.Equals(enUsCultureInfo))
                        continue;
                    if (keyPair.Key == null || string.IsNullOrEmpty(keyPair.Value))
                        continue;

                    if (monospaced)
                    {
                        Font font = new Font(keyPair.Value, 12);
                        if (TextRenderer.MeasureText("X", font).Width
                            != TextRenderer.MeasureText("I", font).Width)
                            continue;
                        font.Dispose();
                    }

                    Items.Add(keyPair.Value);
                }

            }
            selectedIndex = Items.IndexOf(selectedFont);
        }



        protected override void OnComboBoxListOpening()
        {
            base.OnComboBoxListOpening();

        }
        protected string selectedFont;

        [Browsable(true)]
        [Description("当前选中的颜色")]
        [Category("自定义属性")]
        public string SelectedFont
        {
            get
            {
                return selectedFont;
            }
            set
            {
                selectedFont = value;
                selectedIndex = Items.IndexOf(value);
                OnSelectFontChanged(EventArgs.Empty);
            }
        }
        [Browsable(true)]
        [Description("是否只显示等宽字体")]
        [Category("自定义属性")]
        protected bool Monospaced
        {
            get => monospaced;
            set
            {
                monospaced = value;
                UpdateData();
            }
        }

        protected virtual void OnSelectFontChanged(EventArgs eventArgs)
        {
            SelectFontChanged?.Invoke(this, eventArgs);
        }
        protected override void OnDrawItem(DrawItemEventArgs eventArgs)
        {
            base.OnDrawItem(eventArgs);
            Graphics graphics = eventArgs.Graphics;
            int index = eventArgs.Index;
            Rectangle bounds = eventArgs.Bounds;
            if (index == SelectedIndex)
            {
                using (SolidBrush solidBrush = new SolidBrush(SystemColors.HotTrack))
                {
                    graphics.FillRectangle(solidBrush, bounds);
                }
            }
            else if (eventArgs.Hovered)
            {
                using (SolidBrush solidBrush = new SolidBrush(System.Drawing.Color.FromArgb(235, 235, 235)))
                {
                    graphics.FillRectangle(solidBrush, bounds);
                }
            }
            //if (eventArgs.Hovered)
            //{
            //   SolidBrush solidBrush = new SolidBrush(System.Drawing.Color.FromArgb(235, 235, 235));
            //   graphics.FillRectangle(solidBrush, bounds);
            //   solidBrush.Dispose();
            // }


            SolidBrush brush = new SolidBrush(index == SelectedIndex ? System.Drawing.Color.White: ForeColor);
            string vlaue = Items[index] as string;

            Rectangle textRectangle = new Rectangle(
                bounds.X + Padding.Left,
                bounds.Y,
                bounds.Width - Padding.Left - Padding.Right,
                bounds.Height);

            Font font = new Font(vlaue, Font.Size);
            graphics.DrawString(vlaue, font, brush, textRectangle, stringFormat);
            font.Dispose();
            brush.Dispose();
        }

        protected override void OnDrawContent(DrawComboBoxContentEventArgs eventArgs)
        {
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            SolidBrush brush = new SolidBrush(ForeColor);


            eventArgs.Graphics.DrawString(SelectedFont, Font, brush, eventArgs.Bounds, stringFormat);
            pen.Dispose();
            brush.Dispose();
        }

        protected override void OnSelectedIndexChanged(EventArgs eventArgs)
        {
            base.OnSelectedIndexChanged(eventArgs);
            SelectedFont = Items[SelectedIndex] as string;
            // Console.WriteLine("aa" + SelectedIndex);
        }
    }
}
